#include <iostream>

using namespace std;
#include "wall.h"
#include "snake.h"
#include "food.h"
#include <ctime>
#include <curses.h>
#include <unistd.h>

int main()
{
    //添加随机种子
    srand((unsigned int)time(NULL));

    //是否死亡的标识
    bool isDead = false;

    char preKey = NULL;
    Wall wall;
    wall.initWall();
    wall.drawWall();

    Food food(wall);
    food.setFood();

    Snake snake(wall,food);
    snake.InitSnake();

    ::move(0, Wall::ROW);
    cout << "得分：" << snake.getScore() << "分" << endl;
    // gotoxy(hOut, 10, 5);//y*2 x
    // 接受用户的输入

    while (!isDead)
    {
        char key = getch();
        //判断如果是第一次按了，左键，才不能激活游戏
        //判断上一次移动方向
        if (preKey == NULL&&key == snake.LEFT)
        {
            continue;
        }

        while (true) {
            if (key == snake.UP || key == snake.DOWN || key == snake.LEFT || key == snake.RIGHT)
            {
                //判断本次按键是否与上次冲突
                if ((key == snake.LEFT&&preKey == snake.RIGHT )||
                    (key == snake.RIGHT&&preKey == snake.LEFT )||
                    (key == snake.UP&&preKey == snake.DOWN) ||
                    (key == snake.DOWN&&preKey == snake.UP)
                        )
                {
                    key = preKey;
                }
                else
                {
                    preKey = key;//不是冲突按键，可以更新按键
                }

                if (snake.move(key))
                {
                    //移动成功
                    //system("cls");
                    //wall.drawWall();
                    ::move(0, Wall::ROW);

                    cout << "得分：" << snake.getScore() << "分" << endl;
                    sleep(snake.getSleepTime());
                }
                else
                {
                    isDead = true;
                    break;
                }
            }
            else
            {
                key = preKey;//强制将错误按键变为上一次移动的方向
            }

        }

    }
    system("pause");
    return 0;
}
